package org.springcloud.wisdom.spring_02_classroom.admin.createcourse.service;

import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.dao.CourseDao;
import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.entity.Course;
import org.springcloud.wisdom.spring_02_classroom.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class CourseService {
    @Autowired
    CourseDao courseDao;
    // 使用 AtomicInteger 确保线程安全
    private static final AtomicInteger COURSE_COUNT = new AtomicInteger(0);

    //开启选课
    public boolean updatecan(Course course) {
        if(course.getIscanlook().equals("1")){
            return false;
        }
        int result = courseDao.updatecan(course);
        return result > 0;
    }

    //关闭选课
    public boolean updatecant(Course course) {
        if(course.getIscanlook().equals("0")){
            return false;
        }
        int result = courseDao.updatecant(course);
        return result > 0;
    }

    //添加课程
    /**
     * 生成唯一的课程号 cid。
     * @return 生成的课程号
     */
    private String generateCid() {
        // 从数据库中获取最大的 cid 值
        String maxCid = courseDao.getMaxCid();
        if (maxCid!= null) {
            int num = Integer.parseInt(maxCid.substring(1));
            num++;
            return "C" + String.format("%03d", num);
        } else {
            return "C001";
        }
    }

    // 服务层中添加检查逻辑
    public boolean isConflict(Course course) {
        // 排除当前课程的cid
        String currentCid = course.getCid();
        return courseDao.checkTimeLocationConflict(course.getTid(), course.getLtime(), course.getLocation(), currentCid) > 0;
    }

    public boolean isConflict2(Course course) {
        String currentCid = course.getCid();
        return courseDao.checkLocationTimeConflict(course.getLocation(), course.getLtime(), currentCid) > 0;
    }

    public boolean isConflict3(Course course) {
        String currentCid = course.getCid();
        return courseDao.checkTidTimeConflict(course.getTid(), course.getLtime(), currentCid) > 0;
    }


    public boolean addCourse(Course course) {
        // 输出课程信息，以便查看输入数据是否正确
        System.out.println("Received course for addition: " + course);
        if (course == null){
            System.out.println("Course is null");
            return false;
        }if (course.getCname()==null || course.getCname().trim().length() == 0){
            System.out.println("Cname is null");
            return false;
        }if (course.getTid()==null || course.getTid().trim().length() == 0){
            System.out.println("Tid is null");
            return false;
        }if (course.getLtime()==null || course.getLtime().trim().length() == 0){
            System.out.println("Ltime is null");
            return false;
        }if (course.getLocation()==null || course.getLocation().trim().length() ==0){
            System.out.println("Location is null");
            return false;
        }if (course.getChour()==null || course.getChour().trim().length() ==0){
            System.out.println("Chour is null");
            return false;
        }
        if (isConflict(course)) {
            System.out.println("isConflict");
            return false;
        }
        if (isConflict2(course)) {
            System.out.println("isConflict2");
            return false;
        }
        if (isConflict3(course)) {
            System.out.println("isConflict3");
            return false;
        }
        // 自动生成 cid 并设置
        course.setCid(generateCid());
        System.out.println("Generated cid: " + course.getCid());  // 新增这行日志，查看生成的 cid

        // 调用 DAO 层添加课程
        int result = courseDao.insertCourse(course);
        return result > 0;
    }

    //修改课程信息
    public boolean updateCourse(Course course) {
        if (course == null || course.getCid() == null || course.getCid().trim().length() != 4) {
            return false;
        }if (course.getCname()==null || course.getCname().trim().length() == 0){
            return false;
        }if (course.getTid()==null || course.getTid().trim().length() == 0){
            return false;
        }if (course.getLtime()==null || course.getLtime().trim().length() == 0){
            return false;
        }if (course.getLocation()==null || course.getLocation().trim().length() ==0){
            return false;
        }if (course.getChour()==null || course.getChour().trim().length() ==0){
            return false;
        }
        // 先进行校验，是否冲突
        if (isConflict(course)) {
            return false;
        }
        if (isConflict2(course)) {
            return false;
        }
        if (isConflict3(course)) {
            return false;
        }
        // 执行更新操作
        int result = courseDao.updateCourse(course);
        // 检查更新操作是否影响到了数据（即是否有更新发生）
        return result > 0;
    }

    //删除课程
    public boolean deleteCourse(String cid) {
        if (cid == null) {
            return false;
        }
        int result = courseDao.deleteCourse(cid, true);
        return result > 0;
    }


    //分页条件查课程
    public Page<Course> AllCourse(int pageNum, int pageSize, String cid,String tid) {
        // 计算起始位置
        int start = (pageNum - 1) * pageSize;
        // 获取分页的数据
        List<Course> list = courseDao.get(cid,tid, start, pageSize);
        // 获取总条目数
        int total = courseDao.Count(cid,tid);
        // 计算总页数
        int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        // 创建分页对象
        Page<Course> page = new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //历史
    public Page<Course> AllCourseH(int pageNum, int pageSize, String cid,String tid) {
        // 计算起始位置
        int start = (pageNum - 1) * pageSize;
        // 获取分页的数据
        List<Course> list = courseDao.getH(cid,tid, start, pageSize);
        // 获取总条目数
        int total = courseDao.CountH(cid,tid);
        // 计算总页数
        int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        // 创建分页对象
        Page<Course> page = new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}
